#include "contig.h"

#include "cmath"

Contig::Contig(QPointer<SageVis> sageVisPointer, MultiGeneRegionVisWidget* parentWidget, QGraphicsItem *parent ) :
    VisBaseContainer( sageVisPointer, dynamic_cast<VisBaseWidget*> (parentWidget), parent)
{
    uniqueId = "";//TO DO: not unique
    init();
}

Contig::Contig(QPointer<ContigDataPacket> dataPacket ,QPointer<SageVis> sageVisPointer, MultiGeneRegionVisWidget* parentWidget, QGraphicsItem *parent ) :
    VisBaseContainer( sageVisPointer, dynamic_cast<VisBaseWidget*> (parentWidget), parent)
{
    setDataPacket( dataPacket );
    uniqueId = dataPacket->getContigId();
    init();
}

Contig::~Contig()
{
    qDebug() << "deleting contig" << endl;
    while( ! genomeElements.isEmpty() )
    {
        QPointer<GenomeElement> g = genomeElements.takeFirst();
        if( !g.isNull() )
        {
            delete g;
            g = 0;
        }
    }
}

QString Contig::printOutContigData()
{
    return dataPacket->printOutContigData();
}



void Contig::init()
{
    setCursor( Qt::ArrowCursor );//make sure arrow cursor is shown over contig
    //setAcceptsHoverEvents(true);//when hovering over a contig, make it more prominent

    //setFlag( QGraphicsItem::ItemClipsChildrenToShape );//contigs stay in bounds

    maximizedHeight = 15.0;
    defaultHeight = 5.0;

    ntPerPixel = 10.0; //default
    deltaX = 0;
    contigStartX = boundingRect().x();//NOTE: changed!!

    //rectToDraw = new QGraphicsRectItem(this);
    flipped = false;

    setCacheMode(QGraphicsItem::ItemCoordinateCache);//this call is magic- render to pixmap makes it faster

}

//------------------------------SETTERS-------------------------------
void Contig::setDataPacket(QPointer<ContigDataPacket> dataPacket)
{
    uniqueId = dataPacket->getContigId();
   this->dataPacket = dataPacket;
}

//NOTE- later may want to change this so that I am not
void Contig::addGenomeElement( GenomeElement* genomeElement )
{
    //forgeConnection(this, genomeElement, HEIGHT_CONNECTION );//genome elements should be same height as parent

    //genomeElement->setIdx(genomeElements.size());
    genomeElements.append( genomeElement );
    elements.append( genomeElement );

    //see if it overlaps with previous cds
    if( genomeElements.size() > 1 )
    {
        GenomeElement* prev = genomeElements[genomeElements.size()-2];
        if( prev->getStop() > genomeElement->getStart() )//overlap
        {
            //qDebug() << "overlap: " << prev->getStop() << " " << genomeElement->getStart() << endl;
            //if( prev->getStop() > genomeElement->getStop() )
            //    qDebug() << "       complete overlap: " << endl;
        }
    }
}

void Contig::setNtPerPixel(float npp)
{
    ntPerPixel = npp;
}

void Contig::setDeltaX(float dx)
{
    if( dataPacket->getContigId().compare("NZ_AEHN01000004.1") == 0 )
        qDebug() << "       reseting delta x: old:" << deltaX << " new: " << deltaX+dx << "contig id: " << dataPacket->getContigId() << endl;
    deltaX += dx;
}

void Contig::resetDeltaX()
{
    LayoutGenomeElements* l = dynamic_cast< LayoutGenomeElements* > ( layout );//get layout
    //ntPerPixel = l->getNtPerPixel();//find out current nt/pixel

    //get deltaNt

    //compute new start and stop nt

    //if new start and stop nt out of bounds, shift startX and stopX


}


void Contig::targetElement(QString eltId, QColor leftColor, QColor rightColor, QColor startColor)
{
    //first find the elt
    int idx = 0;
    for(int i = 0; i < genomeElements.size(); i++)
    {
        if( eltId.compare(genomeElements[i]->getUniqueId()) == 0 )
        {
            idx = i;
            break;
        }
    }

    qDebug() << "idx = " << idx << endl;

    float gradientHalfLength = 35.0;
    int start = idx - gradientHalfLength;
    if( start < 0 )
        start = 0;
    int end = idx + gradientHalfLength;
    if( end >= genomeElements.size() )
        end = genomeElements.size()-1;

    //center color:  255.0, 208.0, 38.0, 255.0
    //right color:  101.0, 88.0, 255.0
    //QColor startColor;
    //startColor.setRgb(255.0, 208.0, 38.0, 255.0);
    //QColor rightColor;
    //rightColor.setRgb(101.0, 88.0, 255.0, 255.0);

    float deltaR = (rightColor.red()-startColor.red())/gradientHalfLength;
    float deltaG = (rightColor.green()-startColor.green())/gradientHalfLength;
    float deltaB = (rightColor.blue()-startColor.blue())/gradientHalfLength;
    qDebug() << "deltaR " << deltaR << " deltaG " << deltaG << " deltaB " << deltaB << endl;
    QColor prevColor;
    prevColor.setRgb(startColor.red(), startColor.green(), startColor.blue());
    for(int i = idx+1; i <= end; i++)
    {
        QColor newColor;
        newColor.setRgb(prevColor.red()+deltaR, prevColor.green()+deltaG, prevColor.blue()+deltaB );
        genomeElements[i]->setColor(newColor);
        prevColor.setRgb( newColor.red(), newColor.green(), newColor.blue(), newColor.alpha());

        qDebug() << "r " << newColor.red() << " g " << newColor.green() << " b " << newColor.blue() << endl;
    }

    //QColor leftColor;
    //leftColor.setRgb(13.0, 153.0, 73.0, 255.0);
    deltaR = (leftColor.red() - startColor.red())/gradientHalfLength;
    deltaG = (leftColor.green() - startColor.green())/gradientHalfLength;
    deltaB = (leftColor.blue() - startColor.blue())/gradientHalfLength;

    prevColor.setRgb(startColor.red(), startColor.green(), startColor.blue());
    for(int i = idx-1; i >=start; i--)
    {
        QColor newColor;
        //float newRed =
        newColor.setRgb(prevColor.red()+deltaR, prevColor.green()+deltaG, prevColor.blue()+deltaB );
        genomeElements[i]->setColor(newColor);
        prevColor.setRgb( newColor.red(), newColor.green(), newColor.blue(), newColor.alpha());

        qDebug() << "r " << newColor.red() << " g " << newColor.green() << " b " << newColor.blue() << endl;
    }
}

//void Contig::computeNextColor(QColor currColor, QColor deltaColor)
//{

//}

void Contig::targetElement(QString eltId, QColor leftColor, QColor rightColor)
{
    //first find the elt
    int idx = 0;
    for(int i = 0; i < genomeElements.size(); i++)
    {
        if( eltId.compare(genomeElements[i]->getUniqueId()) == 0 )
        {
            idx = i;
            break;
        }
    }

    qDebug() << "idx = " << idx << endl;

//    float gradientHalfLength = 20.0;
    float gradientHalfLength = 35.0;
    int start = idx - gradientHalfLength;
    if( start < 0 )
        start = 0;
    int end = idx + gradientHalfLength;
    if( end >= genomeElements.size() )
        end = genomeElements.size()-1;

    //center color:  255.0, 208.0, 38.0, 255.0
    //right color:  101.0, 88.0, 255.0
    QColor startColor;
    startColor.setRgb(255.0, 208.0, 38.0, 255.0);
    //QColor rightColor;
    //rightColor.setRgb(101.0, 88.0, 255.0, 255.0);

    float deltaR = (rightColor.red()-startColor.red())/gradientHalfLength;
    float deltaG = (rightColor.green()-startColor.green())/gradientHalfLength;
    float deltaB = (rightColor.blue()-startColor.blue())/gradientHalfLength;
    qDebug() << "deltaR " << deltaR << " deltaG " << deltaG << " deltaB " << deltaB << endl;
    QColor prevColor;
    prevColor.setRgb(startColor.red(), startColor.green(), startColor.blue());
    for(int i = idx+1; i <= end; i++)
    {
        QColor newColor;
        newColor.setRgb(prevColor.red()+deltaR, prevColor.green()+deltaG, prevColor.blue()+deltaB );
        genomeElements[i]->setColor(newColor);
        prevColor.setRgb( newColor.red(), newColor.green(), newColor.blue(), newColor.alpha());

        qDebug() << "r " << newColor.red() << " g " << newColor.green() << " b " << newColor.blue() << endl;
    }

    //QColor leftColor;
    //leftColor.setRgb(13.0, 153.0, 73.0, 255.0);
    deltaR = (leftColor.red() - startColor.red())/gradientHalfLength;
    deltaG = (leftColor.green() - startColor.green())/gradientHalfLength;
    deltaB = (leftColor.blue() - startColor.blue())/gradientHalfLength;

    prevColor.setRgb(startColor.red(), startColor.green(), startColor.blue());
    for(int i = idx-1; i >=start; i--)
    {
        QColor newColor;
        newColor.setRgb(prevColor.red()+deltaR, prevColor.green()+deltaG, prevColor.blue()+deltaB );
        genomeElements[i]->setColor(newColor);
        prevColor.setRgb( newColor.red(), newColor.green(), newColor.blue(), newColor.alpha());

        qDebug() << "r " << newColor.red() << " g " << newColor.green() << " b " << newColor.blue() << endl;
    }
}

void Contig::targetElement(QString eltId)
{
    //first find the elt
    int idx = 0;
    for(int i = 0; i < genomeElements.size(); i++)
    {
        if( eltId.compare(genomeElements[i]->getUniqueId()) == 0 )
        {
            idx = i;
            break;
        }
    }

    qDebug() << "idx = " << idx << endl;

    //float gradientHalfLength = 20.0;
    float gradientHalfLength = 35.0;
    int start = idx - gradientHalfLength;
    if( start < 0 )
        start = 0;
    int end = idx + gradientHalfLength;
    if( end >= genomeElements.size() )
        end = genomeElements.size()-1;

    //center color:  255.0, 208.0, 38.0, 255.0
    //right color:  101.0, 88.0, 255.0
    QColor startColor;
    startColor.setRgb(255.0, 208.0, 38.0, 255.0);
    QColor rightColor;
    rightColor.setRgb(101.0, 88.0, 255.0, 255.0);

    float deltaR = (rightColor.red()-startColor.red())/gradientHalfLength;
    float deltaG = (rightColor.green()-startColor.green())/gradientHalfLength;
    float deltaB = (rightColor.blue()-startColor.blue())/gradientHalfLength;
    qDebug() << "deltaR " << deltaR << " deltaG " << deltaG << " deltaB " << deltaB << endl;
    QColor prevColor;
    prevColor.setRgb(startColor.red(), startColor.green(), startColor.blue());
    for(int i = idx+1; i <= end; i++)
    {
        QColor newColor;
        newColor.setRgb(prevColor.red()+deltaR, prevColor.green()+deltaG, prevColor.blue()+deltaB );
        genomeElements[i]->setColor(newColor);
        prevColor.setRgb( newColor.red(), newColor.green(), newColor.blue(), newColor.alpha());

        qDebug() << "r " << newColor.red() << " g " << newColor.green() << " b " << newColor.blue() << endl;
    }

    QColor leftColor;
    leftColor.setRgb(13.0, 153.0, 73.0, 255.0);
    deltaR = (leftColor.red() - startColor.red())/gradientHalfLength;
    deltaG = (leftColor.green() - startColor.green())/gradientHalfLength;
    deltaB = (leftColor.blue() - startColor.blue())/gradientHalfLength;

    prevColor.setRgb(startColor.red(), startColor.green(), startColor.blue());
    for(int i = idx-1; i >=start; i--)
    {
        QColor newColor;
        newColor.setRgb(prevColor.red()+deltaR, prevColor.green()+deltaG, prevColor.blue()+deltaB );
        genomeElements[i]->setColor(newColor);
        prevColor.setRgb( newColor.red(), newColor.green(), newColor.blue(), newColor.alpha());

        qDebug() << "r " << newColor.red() << " g " << newColor.green() << " b " << newColor.blue() << endl;
    }


}

int Contig::getContigLength()
{
    return dataPacket->getLength();
}

void Contig::setContigStartX(float newContigStartX)
{
    contigStartX = newContigStartX;
}

QString Contig::getContigId()
{
    return dataPacket->getContigId();
}

QString Contig::getUniqueId()
{
    return uniqueId;//dataPacket->getContigId();
}

bool Contig::getFlipped()
{
    return flipped;
}

void Contig::setFlipped( bool f )
{
    flipped = f;
}

//-----------------SIZE AND SHAPE FUNCTIONS----------------------------

//void Contig::setGeometry(const QRectF &geom)
//{
//    prepareGeometryChange();
//    QGraphicsWidget::setGeometry(geom);
//    setPos(geom.topLeft());
//}


//QRectF Contig::boundingRect() const
//{
//    return QRectF(QPointF(0,0), geometry().size());
//}


//QPainterPath Contig::shape() const
//{
//    QPainterPath path;
//    path.addRect(boundingRect());
//    return path;
//}


//QSizeF Contig::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
//{
//    switch (which) {
//    case Qt::MinimumSize:
//        return QSizeF(500, 10);
//    case Qt::PreferredSize:
//        return QSizeF(500, 20);
//    case Qt::MaximumSize:
//        return QSizeF(500, 30);
//    default:
//        break;
//    }
//    return constraint;
//}

//-----------------------------------PROCESS USER INPUT-------------------------------------
void Contig::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    float newHeight = geometry().height();
    defaultHeight = newHeight; //store old height
    newHeight *= 3.0;//increase size if hovering
    if( newHeight > maximizedHeight )//ensure it doesn't get too big
        newHeight = maximizedHeight;

    setHeight( newHeight );//set the height

    //qDebug() << "contig processes hover event " << dataPacket->getContigId() << " " << hovering << endl;

    super::processHoverEvent(event); //call base class to ensure hover event goes through
    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

void Contig::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    float newHeight = geometry().height();

    newHeight = defaultHeight;//restore default //3.0;//decrease size if no longer hovering

    setHeight( newHeight );//set the height

    //qDebug() << "contig processes hover event " << dataPacket->getContigId() << " " << hovering << endl;

    super::processHoverEvent(event); //call base class to ensure hover event goes through
    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

void Contig::processHoverEvent(QGraphicsSceneHoverEvent *event)
{
//    //bool willBeHovering = boundingRect().contains(event->pos());
//    //if( boundingRect().contains(event->pos()) )
//    //    hovering = true;
//    //else
//    //    hovering = false;
//    bool willBeHovering = (event->type() == QEvent::HoverEnter);

//    float newHeight = geometry().height();
//    if( willBeHovering )//!hovering )//not hovering now, will soon
//    {
//        defaultHeight = newHeight; //store old height
//        newHeight *= 3.0;//increase size if hovering
//        if( newHeight > maximizedHeight )//ensure it doesn't get too big
//            newHeight = maximizedHeight;
//    }
//    else
//    {
//        newHeight = defaultHeight;//restore default //3.0;//decrease size if no longer hovering
//    }

//    setHeight( newHeight );//set the height

//    //qDebug() << "contig processes hover event " << dataPacket->getContigId() << " " << hovering << endl;

//    super::processHoverEvent(event); //call base class to ensure hover event goes through
//    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

void Contig::receiveHoverEvent(QGraphicsSceneHoverEvent *event)
{
    super::receiveHoverEvent(event); //call base class to ensure hover event goes through
    //qDebug() << "contig receive's hover event " << hovering << endl;

    //for(int i = 0 ; i < genomeElements.size(); i++)
        //genomeElements[i]->receiveHoverEvent(event);//never worked

    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

void Contig::swapGeneDrawDirection()
{
    for(int i = 0; i < genomeElements.size(); i++)
    {
        genomeElements[i]->setDrawForward( !genomeElements[i]->getDrawForward() );
    }
}

//void Contig::alignNtTo(float xPos, int nt, bool flip)
//{
//    LayoutGenomeElements* l = dynamic_cast<LayoutGenomeElements*> (getLayout());
//    float startNt = l->getStartNt();
//    float stopNt = l->getStopNt();
//    float startX = l->getStartX();
//    float stopX = l->getStopX();
//    float contigLength = dataPacket->getLength();

//    float mappedStartX = mapToScene(QPointF(startX,0)).x();
//    float mappedStopX = mapToScene(QPointF(stopX, 0)).x();

//    if( nt > contigLength )//should never happen
//        return;

//    //what nt is that xPos
//    int targetNt = (float)(xPos-mappedStartX)/(float)(mappedStopX-mappedStartX) * (float)(stopNt-startNt) + startNt;
//    qDebug() << "xPos: " << xPos << " mappedStartX: " << mappedStartX << " mappedStopX: " << mappedStopX << " startNt: " << startNt << " stopNt: " << stopNt << " nt: " << nt << " targetNt " << targetNt << endl;

//    if( nt <= stopNt && nt >= startNt )
//    {
//        //compute nt for that xPos
//        float targetNt = (xPos-mappedStartX)/(mappedStopX-mappedStartX) * (stopNt-startNt) + startNt;
//        //float targetNt = (stopX-xPos)/(stopX-startX) * (stopNt-startNt) + startNt;

//        float deltaNt = nt - targetNt;
//        float deltaX = -1*(float)deltaNt/(float)ntPerPixel;
//        moveBy(deltaX, deltaNt);
//        qDebug() << "shifting over " << targetNt << " " << nt << " " << deltaX << " " << deltaNt << endl;
//        layoutElementsNonThreaded();
//        update();
//    }


//    //if xPos is between startX and stopX
////    if( xPos <= mappedStopX && xPos >= mappedStartX )
////    {
////        //compute nt for that xPos
////        float targetNt = (xPos-mappedStartX)/(mappedStopX-mappedStartX) * (stopNt-startNt) + startNt;
////        //float targetNt = (stopX-xPos)/(stopX-startX) * (stopNt-startNt) + startNt;

////        float deltaNt = nt - targetNt;
////        float deltaX = -1*(float)deltaNt/(float)ntPerPixel;
////        moveBy(deltaX, deltaNt);
////        qDebug() << "shifting over " << targetNt << " " << nt << " " << deltaX << " " << deltaNt << endl;
////        layoutElementsNonThreaded();
////        update();
////    }
////    else if( xPos > mappedStopX )
////    {
////        float distToXPos = xPos - mappedStopX; //first move stop pos to target
////        float targetNt = contigLength;
////        float deltaNt = -1* (targetNt-nt + distToXPos*ntPerPixel);
////        float deltaX = -1*(float)deltaNt/(float)ntPerPixel;
////        moveBy(deltaX, deltaNt);
////        qDebug() << "shifting over " << targetNt << " " << nt << " " << deltaX << " " << deltaNt << endl;
////        layoutElementsNonThreaded();
////        update();
////    }
////    else if( xPos < mappedStart )
////    {
////        float distToXPos = mappedStartX - xPos; //first move stop pos to target
////        float targetNt = 0;
////        float deltaNt = nt - targetNt + distToXPos*ntPerPixel;
////        float deltaX = -1*(float)deltaNt/(float)ntPerPixel;
////        moveBy(deltaX, deltaNt);
////        qDebug() << "shifting over " <<  targetNt << " " << nt << " " << deltaX << " " << deltaNt << endl;
////        layoutElementsNonThreaded();
////        update();
////    }





//}

void Contig::moveBy(float deltaX)
{
    LayoutGenomeElements* l = dynamic_cast<LayoutGenomeElements*> (getLayout());
    float startX = l->getStartX();
    float stopX = l->getStopX();

    l->setStartX( startX + deltaX );
    l->setStopX( stopX + deltaX );

}

void Contig::flip()
{
    flipped = !flipped;

    LayoutGenomeElements* l = dynamic_cast<LayoutGenomeElements*> (getLayout());
    float startNt = l->getStartNt();
    float stopNt = l->getStopNt();

    l->setStartNt( stopNt );
    l->setStopNt( startNt );
    layoutElementsNonThreaded();

    swapGeneDrawDirection();

    update();
}

void Contig::zoom(float newNtPerPixel)
{
    LayoutGenomeElements* l = dynamic_cast<LayoutGenomeElements*> (getLayout());

    float lengthX = l->getStopX() - l->getStartX();

    //want to move stopX and startX by 1/2 the diff
    float diffNtPerPixel = newNtPerPixel - ntPerPixel;
    float percentChange = diffNtPerPixel / ntPerPixel;
    float deltaX = lengthX * percentChange;

    //l->setStartX( l->getStartX() //- deltaX/2.0 );
    l->setStopX(l->getStopX() +deltaX);//+ deltaX/2.0 );

    setNtPerPixel(newNtPerPixel);
    l->setNtPerPixel(newNtPerPixel);

    layoutElementsNonThreaded();

    update();
}


//void Contig::moveBy(float deltaX, float deltaNt)
//{
//    LayoutGenomeElements* l = dynamic_cast<LayoutGenomeElements*> (getLayout());
//    float startNt = l->getStartNt();
//    float stopNt = l->getStopNt();
//    float startX = l->getStartX();
//    float stopX = l->getStopX();
//    float contigLength = dataPacket->getLength();
//    ntPerPixel = l->getNtPerPixel();

//    float newStartNt = startNt;// + deltaNt;
//    float newStopNt = stopNt;// + deltaNt;
//    float newStartX = startX;// + deltaX;
//    float newStopX = stopX;// + deltaX;

//    //qDebug() << "BEFORE: startX: " << newStartX << " stopX: " << newStopX << " startNt: " << newStartNt << " stopNt: " << newStopNt << " deltaX: " << deltaX << " deltaNt: " << deltaNt << endl;
////    if(  ( newStopNt-newStartNt )/(newStopX-newStartX) !=  ntPerPixel )
////    {
////        qDebug() << "BEFORE: CHANGE IN NT PER PIXEL " << ( newStopNt-newStartNt )/(newStopX-newStartX)  << " " << ntPerPixel << endl;
////    }


//    //int xDist = stopX-startX;
//    //int ntDist = stopNt-startNt;

//    if( deltaX > 0 )// ----->
//    {
//        //qDebug() << "Moving right!" << endl;

//        //-------------------------------
//        //START NT and START X
//        //is startNt larger than 0?   [ |     |
//        if( startNt > 0 ) //ex:  startNt =100
//        {
//            //qDebug() << "   startNt > 0 " << endl;
//            //does startNt + deltaNt result in move X or just move Nt
//            if( startNt + deltaNt >= 0 )//if moving nt doesn't move out of bounds: 100 + (-10)
//            {
//                newStartNt = startNt + deltaNt; //just adjust start nt
//                //qDebug() << "       just changing start nt:  old: " << startNt << " new: " << newStartNt << endl;
//            }
//            else //else must move startNt to 0, then adjust startX:  100 + (-200)
//            {
//                newStartNt = 0;
//                float adjustedDeltaNt = startNt + deltaNt; //resolve to -100
//                float adjustedDeltaX = deltaX + adjustedDeltaNt/ntPerPixel; //deltaX + -100/ntPerPixel
//                newStartX = startX + adjustedDeltaX;

//                //qDebug() << "       changing both nt and x:  oldNT: " << startNt << " newNt: " << newStartNt << " oldX: " << startX << " newX: " << newStartX << endl;
//            }
//        }
//        else //startNt == 0     | [   |
//        {
//            //qDebug() << "   startNt == 0 " << endl;

//            //if( startNt < 0 )
//            //    qDebug() << "       START NT TOO LOW!!" << startNt << endl;

//            //in this case, X will get repositioned.  no change in startNt
//            if( startX + deltaX  > boundingRect().x() + boundingRect().width() )
//            {
//                //qDebug() << "       startX moving off screen right " << endl;
//                newStartX = boundingRect().x() + boundingRect().width();
//            }
//            else
//            {
//                newStartX = startX + deltaX;
//                //qDebug() << "       moving startX.  old: " << startX << " new: " << newStartX << endl;
//            }
//        }

//        //-------------------------------
//        //STOP NT AND STOP X
//        if( stopX == boundingRect().x() + boundingRect().width() )//if at boundary
//        {
//            //qDebug() << "       just changing stopNt" << endl;
//            //just change stop nt
//            newStopNt = stopNt + deltaNt;
//            //if( newStopNt < 0 )
//            //    newStopNt = 0;

//            //qDebug() << "           what newStopNt should be: " << (newStopX-newStartX)*ntPerPixel + newStartNt << " but it is: " << newStopNt << endl;
//        }
//        else
//        {

//            if( stopX + deltaX < boundingRect().x()+boundingRect().width() )
//            {
//                //qDebug() << "       just changing stopX" << endl;
//                newStopX = stopX + deltaX;
//            }
//            else //change both stopx and stopnt
//            {
//                //qDebug() << "       changing stopX and stopNt" << endl;

//                deltaX = deltaX - ( (boundingRect().x() +boundingRect().width()) - stopX );
//                newStopX = boundingRect().x() +boundingRect().width();
//                deltaNt = -1.0*deltaX*ntPerPixel;
//                newStopNt = deltaNt + stopNt;

//                //if( newStopNt < 0 )
//                //    newStopNt = 0;
//            }

//        }
//    }
//    else if( deltaX < 0 ) // <-----
//    {
//        //-------------------------------
//        //START NT and START X
//        if( startX == boundingRect().x() )//if is on the barrier already, just change startNt
//        {
//            newStartNt = startNt + deltaNt;
//            //if( newStartNt > contigLength )
//            //    newStartNt = contigLength;
//        }
//        else if( startX + deltaX > boundingRect().x() )// if not going to cross barrier, just change deltaX
//        {
//            newStartX = startX + deltaX;
//        }
//        else//going to cross barrier
//        {
//            float adjustedDeltaX = deltaX + (startX - boundingRect().x());
//            newStartX = boundingRect().x();
//            float adjustedDeltaNt = -1.0 * adjustedDeltaX * ntPerPixel;
//            newStartNt = startNt + adjustedDeltaNt;
//            //if( newStartNt > contigLength )
//            //    newStartNt = contigLength;
//        }

//        //-------------------------------
//        //STOP NT AND STOP X
//        if( stopX < boundingRect().x() + boundingRect().width() )//if less than border
//        {
//            //just change stopX
//            newStopX = stopX + deltaX;
//            //if( newStopX < boundingRect().x() )
//            //    newStopX = boundingRect().x();
//        }
//        else if( stopNt + deltaNt <= contigLength )
//        {
//            newStopNt = stopNt + deltaNt;
//        }
//        else
//        {
//            float adjustedDeltaNt = deltaNt - (contigLength - stopNt);
//            newStopNt = contigLength;
//            float adjustedDeltaX = -1.0* adjustedDeltaNt / ntPerPixel;
//            newStopX = stopX + adjustedDeltaX;
//            //if( newStopX < boundingRect().x() )
//            //    newStopX = boundingRect().x();
//        }

//    }

//    //qDebug() << " AFTER: startX: " << newStartX << " stopX: " << newStopX << " startNt: " << newStartNt << " stopNt: " << newStopNt << " deltaX: " << deltaX << " deltaNt: " << deltaNt << endl;

//    l->setStartNt(newStartNt);
//    l->setStopNt(newStopNt);
//    l->setStartX(newStartX);
//    l->setStopX(newStopX);


//}

void Contig::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    //----prevent compiler warnings by declaring what I am not using---
    Q_UNUSED(widget);
    Q_UNUSED(option);

    //---set color---
    QColor backgroundColor;

    if( hovering )
    {
        backgroundColor.setRgb(90.0, 90.0, 90.0);
    }
    else
        backgroundColor.setRgb(60.0, 60.0, 60.0);

    //DRAW CONTIG RECT
    LayoutGenomeElements* l = dynamic_cast< LayoutGenomeElements* > ( layout );//get layout

    //old approach
//    ntPerPixel = l->getNtPerPixel();//find out current nt/pixel
//    float widthToDraw = (float) dataPacket->getLength() / ntPerPixel; //based on ntPerPixel and length, compute width to draw
//    QRectF rectToDraw(deltaX+contigStartX, boundingRect().y(), widthToDraw, boundingRect().height());//set rect
//    if( deltaX < 0.0 )//if moved off screen
//    {
//        widthToDraw = widthToDraw + deltaX; //shorten the width
//        if( widthToDraw < 0.0 ) //if width is negative
//            widthToDraw = 0.0;//set to 0
//        rectToDraw.setRect(0.0, boundingRect().y(), widthToDraw, boundingRect().height() );//reset rect
//    }

    QRectF rectToDraw( l->getStartX(), boundingRect().y(), l->getStopX()-l->getStartX(), boundingRect().height() );

    painter->setBrush(backgroundColor);
    painter->setPen( Qt::NoPen );
    painter->drawRoundedRect(rectToDraw, .25*rectToDraw.height(), .25*rectToDraw.height() );

    //RESET LINE CONNECTION POINT
    float lcpX = rectToDraw.x();
    if( rectToDraw.x() < boundingRect().x() )
        lcpX = boundingRect().x();
    lineConnectionPoint.setX( lcpX );//rectToDraw.x() );
    emit( updatedSignal() );//uplade the line connection pt

    //DRAW LINE CONNECTION PT
    //not drawing on top- must fix
    //painter->setBrush(Qt::white );
    //QPointF lcp = lineConnectionPoint;//mapFromParent(getLineConnectionPoint() );
    //painter->drawEllipse( lcp.x()-2.0, lcp.y()-2.0, 4.0, 4.0 );
}

